let connection = require("../dbutil/dbutil");
let myModule = require("./my-module");
let moment = require("moment");//引入日期格式类库

function getInfo(id, token, callback) {
    let sql = "select username from user where uid = ? and token = ?";
    let sqlparams=[id, token]
    connection.query(sql, sqlparams, function (err, result) {
        if (err) {
            console.log(err);
        } else {
            let ret = {
                code: 1002,
                user: result[0].username,
                money: 0,
                trade: []
            }
            if (result.length > 0 && myModule.verifyToken(token)) {
                let wsql = "select wmoney from wallet where uid = '" + id + "'";
                let tsql = "select * from trade where uid = '" + id + "' ORDER BY ttime DESC";
                let wmoney = 0
                connection.query(wsql, function (err, result) {
                    if (err) {
                        console.log(err);
                    } else {
                        if (result.length > 0) {
                            wmoney = result[0].wmoney
                        }
                        connection.query(tsql, function (err, result) {
                            if (err) {
                                console.log(err);
                            } else {
                                ret.code = 1001
                                ret.money = wmoney,
                                ret.trade = result
                                callback(ret)
                            }
                        })
                    }
                })
            } else {
                callback(ret)
            }
        }
    })
}

function addbalance(id, token, money, callback) {
    let sql = "select username from user where uid = ? and token = ?";
    let sqlparams=[id, token]
    let addMoney = parseInt(money);
    connection.query(sql,sqlparams, function (err, result) {
        if (err) {
            console.log(err);
        } else {
            let ret = {
                code: 1003,
                msg: "充值失败"
            }

            if (result.length > 0 && myModule.verifyToken(token)) { // 验证 token
                let wsql = "select wmoney from wallet where uid = '" + id + "'";
                let wmoney = 0
                connection.query(wsql, function (err, result) {
                    if (err) {
                        console.log(err);
                    } else {
                        wmoney = parseInt(result[0].wmoney) + addMoney
                        if (wmoney > 30000) { // 余额大于 30000，不允许充值
                            ret.code = 1002
                            ret.msg = "余额大于 30000，不允许充值"
                            callback(ret)
                        } else {
                            // wallet
                            let upsql = "UPDATE wallet SET wmoney = '" + wmoney + "' WHERE uid = '" + id + "'"; 
                            connection.query(upsql, function (err) {
                                if (err) {
                                    console.log(err);
                                } else {
                                    // trade 
                                    let time = moment().format('YYYY-MM-DD HH:mm:ss')
                                    let insql = "INSERT INTO `trade` (`uid`, `tmoney`, `type`, `ttime`, `remark`, `balance`) VALUES (?, ?, ?, ?, ?, ?)"
                                    let inSqlParams = [id, money, "充值" , time, "用户充值", wmoney];
                                    connection.query(insql, inSqlParams, function (err, result) {
                                        if (err) {
                                            console.log(err)
                                        } else {
                                            ret.code = 1001
                                            ret.msg = "充值成功"
                                            callback(ret)
                                        }
                                    })

                                }
                            })
                        }
                    }
                })
                
            } else {
                callback(ret)
            }
        }
    })
}


exports.addbalance = addbalance
exports.getInfo = getInfo;